package com.tianzehua.netty.socketexample;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * socket server bootstarp class
 * @author tianzehua
 * @date  2019/03/08
 */
public class MyServer {

    public static void main(String[] args) throws  Exception {

        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        /* netty 的引导类 */
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        /* handler 和 childhandler 的区别： handler 作用的是bossGroup , childhandler 作用的是 workerhandler */
        serverBootstrap.group(bossGroup,workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new MyServerInitializer());

        try {
            ChannelFuture channelFuture = serverBootstrap.bind(8999).sync();
            channelFuture.channel().closeFuture().sync();
        }finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }
}
